; This file is the part of the Textator project
; Last changed: $Id: print.asm 50 2009-02-26 07:48:29Z vitamin.caig $
; (C) Vitamin/CAIG/2001

; Specifies functions, which is able to print tokenized text
; via external output function

; tiny text model (up to 3840 bytes of local texts)
; 2 bytes per token
; Label LocalBase should point to local text start

; SjASM format used

        macro DEFTOKEN id
id      equ $
        endm

        ifdef TINYTEXT
TKNBASE equ 14
TKNEND  equ 31

        macro TESTTOKEN jpto
        cp TKNBASE
        jr c,jpto
        cp TKNEND+1
        jr nc,jpto
        add a,(high LocalBase)-TKNBASE
        ex af,af'
        ld a,(hl)
        inc hl
        endm

        macro TOKEN id
        assert (high id)-(high LocalBase) <= TKNEND-TKNBASE
        db TKNBASE+(high id)-(high LocalBase),low id
        endm

        else
; Huge text model
TKNCODE equ 16

        macro TESTTOKEN jpto
        cp TKNCODE
        jr nz,jpto
        ld a,(hl)
        inc hl
        ex af,af'
        ld a,(hl)
        inc hl
        endm

;BigEndian order due to optimization
        macro TOKEN id
        db TKNCODE,high id,low id
        endm

        endif

; Print tokenized string
; In: hl - text address
; Function StrOutChar should be defined
StrPrint
        xor a
        push af ;limiter
StrCycle
        ld a,(hl)
        inc hl
        and a
        jr z,TestEnd
        TESTTOKEN StrOut
        push hl
        ld l,a
        ex af,af'
        ld h,a
        jr StrCycle
StrOut  call StrOutChar ;external
        jr StrCycle
TestEnd
        pop hl
        ld a,h
        and a
        jr nz,StrCycle
        ret
